package org.raymond.iworks.study.netty.atguigu.nio;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

@Slf4j
public class NioClient {
    public static void main(String[] args) throws Exception {
        String msg = "1234";
        ByteBuffer byteBuffer = ByteBuffer.wrap(msg.getBytes());
        byteBuffer.put(msg.getBytes());

        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        InetSocketAddress inetSocketAddress = new InetSocketAddress(6666);
        // 连接服务端,非阻塞
        if(!socketChannel.connect(inetSocketAddress)){
            while(!socketChannel.finishConnect()){
                log.info("客户端还未连接");
            }
        }

        log.info("客户端连接成功");
        byteBuffer.flip();
        socketChannel.write(byteBuffer);
        while(byteBuffer.hasRemaining()){
            log.info("还未发送完成");
        }
        log.info("发送完毕");
    }
}
